﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using eap.share;
using System.Text.RegularExpressions;
using System.Text;

namespace zsproject.page
{
	public class Score : System.Web.UI.Page
	{
		//存放要传递到aspx文件显示的数据
		public System.Collections.Hashtable output=new System.Collections.Hashtable();
		private string method="";
		public string this_course_type="";
		public string this_para="";

		public string this_url="";
		public string this_year_id="";
		public string this_semester_id="";
		public string this_class_id="";
		public string this_course_id="";
		public string is_copy="";

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			//---------------------------------------------------------------
			Response.AddHeader("Pragma","No-cache");
			Response.AddHeader("Cache-Control", "no-cache");
			Response.Expires=0;
			//---------------------------------------------------------------

			//读取当前方法
			string page=Req.getPara(Request,"page");
			
			method=Req.getPara(Request,"method");
			this_course_type=Req.getPara(Request,"this_course_type");
			if(this_course_type!="")
			{
				this_para="&this_course_type="+this_course_type;
			}

			//初始化系统(基本参数，配置参数，数据库连接参数)
			AdminLib.init(Request,Response,Server);
			//检查用户是否登录，是否有权限
			AdminUser user = (AdminUser)Session["_user"];

			if(user==null || !user.isLogin()){ Response.Redirect("../admin/index.aspx?page=unlogin"); Response.End() ; }
			if(user.isRelogin(Request)){ Response.Redirect("../admin/index.aspx?page=relogin"); Response.End() ; }
			//没有菜单的,特殊链接可以不要
			if(!user.getRight(Request)){ Response.Redirect("../admin/index.aspx?page=erraccess"); Response.End() ; }
			if(!user.isWarning()){ Response.Redirect("../admin/index.aspx?page=warning"); Response.End(); }
			//资源权限
			output.Add("actlist",DAO.sqlValueList("SELECT * FROM  dbo.getRightId("+user["id"]+")")); //保存用户有权限的操作	

			if(page=="List") List(Request,Response,Session,output);
			if(page=="List_action"){List_action(Request,Response,Session,output);}
			if(page=="Form") Form(Request,Response,Session,output);			
			if(page=="Form_action"){Form_action(Request,Response,Session,output);}
		}

	 
		private void List(System.Web.HttpRequest Request, System.Web.HttpResponse Response,System.Web.SessionState.HttpSessionState Session,System.Collections.Hashtable output)
		{
			AdminUser user = (AdminUser)Session["_user"];
			string group_id=Sys.toSQLText(Req.getPara(Request,"group_id"));
			string cond2 = " 1=1 ";      //附加的查询条件

			//查询属于下属
			if(group_id=="")
			{
				//查询所有管辖区域的企业
				group_id= user["dept_id"];				
			}

			
			if(user.getListGroupName().IndexOf("管理")==-1 && user.getListGroupName().IndexOf("教学")==-1)
			{

				if(user.getListGroupName().IndexOf("老师")!=-1)
				{
					cond2=cond2+" and teacher_name='"+user["username"]+"'";
				}

				if(user.getListGroupName().IndexOf("学生")!=-1)
				{
					cond2=cond2+" and student_cn_name='"+user["username"]+"'";
				}
			}

			
			this_year_id=Req.getPara(Request,"this_year_id");
			this_semester_id=Req.getPara(Request,"this_semester_id");
			this_class_id=Req.getPara(Request,"this_class_id");
			this_course_id=Req.getPara(Request,"this_course_id");

			if(this_year_id!="")
			{
				this_url+="&this_year_id="+this_year_id;
				cond2+=" and t_year_id='"+this_year_id+"'";
			}

			if(this_semester_id!="")
			{
				this_url+="&this_semester_id="+this_semester_id;
				cond2+=" and t_semester_id='"+this_semester_id+"'";
			}

			if(this_class_id!="")
			{
				this_url+="&this_class_id="+this_class_id;
				cond2+=" and t_class_id='"+this_class_id+"'";
			}

			if(this_course_id!="")
			{
				this_url+="&this_course_id="+this_course_id;
				cond2+=" and t_course_id='"+this_course_id+"'";
			}

			

			// 设置参数			
			string deforder="";   //缺省的排序条件
			string orderTable="";  //排序字段缺省对应的表名，空表示缺省(主要是因为access数据库查询用join时不指定表名容易报错)
			string table="v_score tb";  //要查询的表名
			string flist = "student_en_name+student_cn_name+teacher_name";    //可以检索的字段， 用'+'分格
			string sfield = "tb.*"; //要在select 语句中查询的字段,空或*表示所有
			string search_title="根据录入的关键字进行检索";  //查询提示
			//标题
			string title="";
			//查询数据库
			AdminList lst=new AdminList(Request,AdminLib.getSimpleRequestURL(Request)+"?page="+Req.getPara(Request,"page"),deforder,user["cfg_list"]);
			System.Data.DataRowCollection list=lst.getList(table,flist,cond2,sfield,orderTable);
			//保存结果
			output.Add("title",title);
			output.Add("search_title",search_title);
			output.Add("list",list);
			output.Add("lst",lst);
		}//end method

		private void List_action(System.Web.HttpRequest Request, System.Web.HttpResponse Response,System.Web.SessionState.HttpSessionState Session,System.Collections.Hashtable output)
		{
			AdminUser user = (AdminUser)Session["_user"];
			int errcode=0;
			string errmsg="";
			string sql = "",message="",strMenu="信息";
			string action=Req.getPara(Request,"action");

			string d=Sys.toSQLText(Req.getPara(Request,"d"));
			if(d!="") d=""+Sys.toInteger(d);

			//检查输入的内容
			//if(title=="") errmsg=errmsg+"标题不能为空。";

			//删除某条记录/删除选中的记录
			if(action=="delete")
			{
				if(d==null||d=="") 
				{
					d=AdminLib.getSelectedCheckbox(Request); //读取选中的记录
				}
            
				if(d==null||d=="")
				{ 
					Response.Write(user.getActionAlarm("您还未选择记录，请选择。")); 
					return; 
				} 
				errcode=errcode+DAO.sqlExec("delete from t_score where id in ("+d+")");
				
				message=strMenu+"(d="+d+")，删除"+(errcode==0?"成功":"出错"+errmsg)+"。";
			}

			if(errmsg!=""){ Response.Write(user.getActionAlarm(errmsg)); return; }

			//Response.Write("<script>alert('"+message+"');</script>");
			if(errcode!=0) message=message+"(errcode="+errcode+")";
			if(errcode!=0) user.saveLog(Request,message); //保存日志
			string reload=errcode==0?Req.getPara(Request,"reload").Replace("{$id}",d):null; //如果发生了错误，则不需要重新刷新
			Response.Write(user.getActionString(Session,errcode,reload,message)); //显示结果 
		} 

		private void Form(System.Web.HttpRequest Request, System.Web.HttpResponse Response,System.Web.SessionState.HttpSessionState Session,System.Collections.Hashtable output)
		{
			AdminUser user = (AdminUser)Session["_user"];
			
			string d=Sys.toSQLText(Req.getPara(Request,"d"));
			if(d!="") d=""+Sys.toInteger(d);

			System.Data.DataRow map=DAO.sqlRow("select * from t_score where id ='"+d+"'");

			is_copy=Req.getPara(Request,"is_copy");
		
			

			if(map!=null)
			{

				string sql="";

				if(this_course_type=="01")
				{
					sql=String.Format(@"select * from t_score_rank where t_student_score_id='{0}'",map["id"].ToString());
				}
				else
				{
					sql=String.Format(@"select * from t_score_fraction where t_student_score_id='{0}'",map["id"].ToString());
				}
				
				System.Data.DataRowCollection list=DAO.sqlList(sql);

				if(is_copy=="true")
				{
					list=null;
					this_course_type=map["score_type"].ToString();

				}

				output.Add("list",list);
			}
			//保存结果
			output.Add("map",map);		 
		}//end method 


		private void Form_action(System.Web.HttpRequest Request, System.Web.HttpResponse Response,System.Web.SessionState.HttpSessionState Session,System.Collections.Hashtable output)
		{
			AdminUser user = (AdminUser)Session["_user"];
			string sql = "",message="",strMenu="信息";
			string errmsg="";
			int errcode=0;
			string action=Req.getPara(Request,"action");

			string d=Sys.toSQLText(Req.getPara(Request,"d"));
			if(d!="") d=""+Sys.toInteger(d);
			//获取页面参数
			string id=Sys.toSQLText(Req.getPara(Request,"id"));
			string t_class_id=Sys.toSQLText(Req.getPara(Request,"t_class_id"));
			string student_name=Sys.toSQLText(Req.getPara(Request,"student_name"));
			string student_num=Sys.toSQLText(Req.getPara(Request,"student_num"));
			string t_year_id=Sys.toSQLText(Req.getPara(Request,"t_year_id"));
			string t_semester_id=Sys.toSQLText(Req.getPara(Request,"t_semester_id"));
			string t_specialty_id=Sys.toSQLText(Req.getPara(Request,"t_specialty_id"));
			string t_course_id=Sys.toSQLText(Req.getPara(Request,"t_course_id"));
			string t_teacher_id=Sys.toSQLText(Req.getPara(Request,"t_teacher_id"));
			string score_records=Sys.toSQLText(Req.getPara(Request,"score_records"));
			string score_usually=Sys.toSQLText(Req.getPara(Request,"score_usually"));
			string per_usually=Sys.toSQLText(Req.getPara(Request,"per_usually"));
			string score_ending=Sys.toSQLText(Req.getPara(Request,"score_ending"));
			string per_ending=Sys.toSQLText(Req.getPara(Request,"per_ending"));
			string score_sum=Sys.toSQLText(Req.getPara(Request,"score_sum"));
			string final=Sys.toSQLText(Req.getPara(Request,"final"));
			string possible_points=Sys.toSQLText(Req.getPara(Request,"possible_points"));
			string credits=Sys.toSQLText(Req.getPara(Request,"credits"));
			string score_type=Sys.toSQLText(Req.getPara(Request,"score_type"));

			//检查输入的内容
			//if(title=="") errmsg=errmsg+"标题不能为空。";

			if(student_num=="") errmsg=errmsg+"请选择学生。";
			if(errmsg!=""){ Response.Write(user.getActionAlarm(errmsg)); return; }		
		
			
			if(action=="edit")
			{
				if(d=="")
				{
					//新增
					sql=@"INSERT INTO t_score
					(						
						[t_class_id] ,
						[student_name] ,
						[student_num] ,
						[t_year_id] ,
						[t_semester_id] ,
						[t_specialty_id] ,
						[t_course_id] ,
						[t_teacher_id] ,
						[score_records] ,
						[score_usually] ,
						[per_usually] ,
						[score_ending] ,
						[per_ending] ,
						[score_sum] ,
						[final] ,
						[possible_points] ,
						[credits] ,
						[score_type] 
					)
					VALUES
					(
						'"+t_class_id+
						"','"+student_name+
						"','"+student_num+
						"','"+t_year_id+
						"','"+t_semester_id+
						"','"+t_specialty_id+
						"','"+t_course_id+
						"','"+t_teacher_id+
						"','"+score_records+
						"','"+score_usually+
						"','"+per_usually+
						"','"+score_ending+
						"','"+per_ending+
						"','"+score_sum+
						"','"+final+
						"','"+possible_points+
						"','"+credits+
						"','"+score_type+
							"');Select @@IDENTITY";
					d = DAO.sqlValue(sql).ToString();

					strMenu=strMenu+"(d="+d+")新增";
				} 
				else
				{	
					//修改
					sql=@"UPDATE t_score
						SET
						[t_class_id] ='"+t_class_id+
						"',[student_name]='"+student_name+
						"',[student_num]='"+student_num+
						"',[t_year_id]='"+t_year_id+
						"',[t_semester_id]='"+t_semester_id+
						"',[t_specialty_id]='"+t_specialty_id+
						"',[t_course_id]='"+t_course_id+
						"',[t_teacher_id]='"+t_teacher_id+
						"',[score_records]='"+score_records+
						"',[score_usually]='"+score_usually+
						"',[per_usually]='"+per_usually+
						"',[score_ending]='"+score_ending+
						"',[per_ending]='"+per_ending+
						"',[score_sum]='"+score_sum+
						"',[final]='"+final+
						"',[possible_points]='"+possible_points+
						"',[credits]='"+credits+
						"',[score_type]='"+score_type+
						"' where id='"+d+"';";
					errcode=DAO.sqlExec(sql);

					strMenu=strMenu+"(d="+d+")修改";
				}

				sql="";
				if(this_course_type=="01")
				{					
					string rank_id_1=Sys.toSQLText(Req.getPara(Request,"rank_id_1"));
					string rank_name_1=Sys.toSQLText(Req.getPara(Request,"rank_name_1"));
					string rank_p_1=Sys.toSQLText(Req.getPara(Request,"rank_p_1"));
					string rank_m1_1=Sys.toSQLText(Req.getPara(Request,"rank_m1_1"));
					string rank_m2_1=Sys.toSQLText(Req.getPara(Request,"rank_m2_1"));
					string rank_m3_1=Sys.toSQLText(Req.getPara(Request,"rank_m3_1"));
					string rank_d1_1=Sys.toSQLText(Req.getPara(Request,"rank_d1_1"));
					string rank_d2_1=Sys.toSQLText(Req.getPara(Request,"rank_d2_1"));
					string rank_d3_1=Sys.toSQLText(Req.getPara(Request,"rank_d3_1"));
					if(rank_id_1=="")
					{
						//新增
						sql+=@"INSERT INTO t_score_rank
								(						
									[t_student_score_id] ,
									[rank_name] ,
									[rank_p] ,
									[rank_m1] ,
									[rank_m2] ,
									[rank_m3] ,
									[rank_d1] ,
									[rank_d2] ,
									[rank_d3] 
								)
								VALUES
								(
								'"+d+
								"','"+rank_name_1+
								"','"+rank_p_1+
								"','"+rank_m1_1+
								"','"+rank_m2_1+
								"','"+rank_m3_1+
								"','"+rank_d1_1+
								"','"+rank_d2_1+
								"','"+rank_d3_1+
								"');Select @@IDENTITY;";
					} 
					else
					{	
						//修改
						sql+=@"UPDATE t_score_rank
								SET
								[t_student_score_id] ='"+d+
								"',[rank_name]='"+rank_name_1+
								"',[rank_p]='"+rank_p_1+
								"',[rank_m1]='"+rank_m1_1+
								"',[rank_m2]='"+rank_m2_1+
								"',[rank_m3]='"+rank_m3_1+
								"',[rank_d1]='"+rank_d1_1+
								"',[rank_d2]='"+rank_d2_1+
								"',[rank_d3]='"+rank_d3_1+
								"' where id='"+rank_id_1+"'";
					}


					string rank_id_2=Sys.toSQLText(Req.getPara(Request,"rank_id_2"));
					string rank_name_2=Sys.toSQLText(Req.getPara(Request,"rank_name_2"));
					string rank_p_2=Sys.toSQLText(Req.getPara(Request,"rank_p_2"));
					string rank_m1_2=Sys.toSQLText(Req.getPara(Request,"rank_m1_2"));
					string rank_m2_2=Sys.toSQLText(Req.getPara(Request,"rank_m2_2"));
					string rank_m3_2=Sys.toSQLText(Req.getPara(Request,"rank_m3_2"));
					string rank_d1_2=Sys.toSQLText(Req.getPara(Request,"rank_d1_2"));
					string rank_d2_2=Sys.toSQLText(Req.getPara(Request,"rank_d2_2"));
					string rank_d3_2=Sys.toSQLText(Req.getPara(Request,"rank_d3_2"));
					if(rank_id_2=="")
					{
						//新增
						sql+=@"INSERT INTO t_score_rank
								(						
									[t_student_score_id] ,
									[rank_name] ,
									[rank_p] ,
									[rank_m1] ,
									[rank_m2] ,
									[rank_m3] ,
									[rank_d1] ,
									[rank_d2] ,
									[rank_d3] 
								)
								VALUES
								(
								'"+d+
								"','"+rank_name_2+
								"','"+rank_p_2+
								"','"+rank_m1_2+
								"','"+rank_m2_2+
								"','"+rank_m3_2+
								"','"+rank_d1_2+
								"','"+rank_d2_2+
								"','"+rank_d3_2+
								"');Select @@IDENTITY;";
					} 
					else
					{	
						//修改
						sql+=@"UPDATE t_score_rank
								SET
								[t_student_score_id] ='"+d+
								"',[rank_name]='"+rank_name_2+
								"',[rank_p]='"+rank_p_2+
								"',[rank_m1]='"+rank_m1_2+
								"',[rank_m2]='"+rank_m2_2+
								"',[rank_m3]='"+rank_m3_2+
								"',[rank_d1]='"+rank_d1_2+
								"',[rank_d2]='"+rank_d2_2+
								"',[rank_d3]='"+rank_d3_2+
								"' where id='"+rank_id_2+"'";
					}
					errcode=DAO.sqlExec(sql);
				
				}

				if(this_course_type=="02")
				{
					
					string[] fraction_id =Sys.toSQLText(Req.getParaValues(Request,"fraction_id"));
					string[] fraction_name =Sys.toSQLText(Req.getParaValues(Request,"fraction_name"));
					string[] fraction_score =Sys.toSQLText(Req.getParaValues(Request,"fraction_score"));
					//string rank_p1 =Sys.toSQLText(Req.getParaList(Request,"rank_p"));

					for(int ii=0;ii<fraction_id.Length;ii++)
					{
						if(fraction_id[ii]=="")
						{
							//新增
							sql+=@"INSERT INTO t_score_fraction
							(						
								[t_student_score_id] ,
								[fraction_name] ,
								[fraction_score] 
							)
							VALUES
							(
								'"+d+
								"','"+fraction_name[ii]+
								"','"+fraction_score[ii]+
								"');Select @@IDENTITY;";
						} 
						else
						{	
							//修改
							sql+=@"UPDATE t_score_fraction
								SET
								[t_student_score_id] ='"+d+
								"',[fraction_name]='"+fraction_name[ii]+
								"',[fraction_score]='"+fraction_score[ii]+
								"' where id='"+fraction_id[ii]+"';";
							
						}
					}
					errcode=DAO.sqlExec(sql);
				
				}



				

				


				message=strMenu+(errcode==0?"成功":"出错")+"。";
			
			}			
			//Response.Write("<script>alert('"+message+"');</script>");
			if(errcode!=0) message=message+"(errcode="+errcode+")";
			if(errcode!=0) user.saveLog(Request,message); //保存日志
			string reload=errcode==0?Req.getPara(Request,"reload").Replace("{$id}",d):null; //如果发生了错误，则不需要重新刷新
			Response.Write(user.getActionString(Session,errcode,reload,message)); //显示结果 
		} 
		
		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
